const CONFIG = require('../../config.js');

Page({
	data: {
		navBgColor: '-webkit-gradient(linear, 0% 0%, 0% 100%,from(#1b64e5), to(#488dea))',
		loginInfo: null,
		orderList: [],
		accumulate: 0,
		formData: {
			payAccumulate: '',
			payMoney: '0',
			payNo: '',
			payType: '',
		},
		isShow: true,
		payTypeList: [{name: '微信赎回', id: '0'},{name: '支付宝赎回', id: '1'},{name: '银行卡赎回', id: '2'}]
	},

  	// 单选
	  radioChange: function (e) {
		const {field} = e.currentTarget.dataset;
		this.setData({
			[`formData.${field}`]: e.currentTarget.dataset.value
		})
	},
	// 输入框
	formInputChange(e) {
		const {field} = e.currentTarget.dataset;
		let val = e.detail.value;
		if(field === 'payAccumulate'){
			if(val > this.data.accumulate){
				wx.showToast({
					title: '提现积分不能大于总积分！',
					icon: 'none',
					duration: 2000
				})
				val = val.substring(0, val.length-1)
			}
			this.setData({
				['formData.payMoney']: val  / 10
			})
		}
		this.setData({
			[`formData.${field}`]: val
		})
	},

	// 获取提现信息列表
	getOrderList(){
		wx.request({
			url: CONFIG.host + "/system/payBackInfoController/appGetPayBackInfoList",
			data: {
				pageNum: 1,
				pageSize: 12
			},
			method: "GET",
			header: {
				'content-type': 'application/json',
				'Authorization': this.data.loginInfo.token
			},
			success: res => {
				if(res.data.code === 200){
					this.setData({
						orderList: res.data.rows,
					})
				}
			},fail: function (error) {
				console.log(error);
			}
		})
	},
	// 获取总积分接口
	getAccount(){
		wx.request({
			url: CONFIG.host + "/getInfoNew",
			method: "GET",
			header: {
				'content-type': 'application/json',
				'Authorization': this.data.loginInfo.token
			},
			success: res => {
				if(res.data.code === 200){
					this.setData({
						accumulate: res.data.user.accumulate,
					})
				}
			},fail: function (error) {
				console.log(error);
			}
		})
	},
	// 显示新增弹框
	showModalFn(){
		this.getAccount();
		this.setData({
			isShow: !this.data.isShow
		})
	},

	// 关闭新增弹框
	closeFn(){
		this.setData({
			isShow: !this.data.isShow
		})
	},

	// 新增提现数据
	submitFn(){
		let datas = this.data.formData;
		if(!datas.payAccumulate || !datas.payNo || !datas.payType){
			wx.showToast({
				title: '请将信息填写完整！',
				icon: 'none',
				duration: 2000
			})
			return;
		}
		wx.request({
			url: CONFIG.host + "/system/payBackInfoController/addPayBackInfo",
			data: datas,
			method: "GET",
			header: {
				'content-type': 'application/json',
				'Authorization': this.data.loginInfo.token
			},
			success: res => {
				wx.showToast({
					title: res.data.message,
					icon: 'none',
					duration: 2000
				})
				if (res.data.code === 'success') {
					this.getOrderList();
					this.showModalFn();
				}
			},fail: function (error) {
				console.log(error);
			}
		})
	},

	// 
	

	onLoad: function () {
		let loginInfo = wx.getStorageSync('loginInfo') || '';
		if(loginInfo){
			this.setData({
				loginInfo : JSON.parse(loginInfo)
			})
			this.getOrderList();
		}
	},
	onShow: function () {
	},
})
